<?php


namespace app\api\controller\xiluhouse;


use app\common\controller\Api;
use app\common\library\xiluhouse\Wxmini;
use app\common\model\xiluhouse\RentalOrder;
use app\common\model\xiluhouse\UserThird;
use think\Db;
use think\Exception;
use think\Log;
use function fast\array_get;

class Pay extends Api
{
    protected $noNeedLogin = ['notify'];
    protected $noNeedRight = ['*'];

    /**
     * 支付订单的待支付信息
     */
    public function pre_pay_order(){
        $rental_order_id = $this->request->param('rental_order_id');
        if(!$rental_order_id){
            $this->error('参数错误');
        }
        $rental_order = RentalOrder::field(['id','order_type','user_id','state','pay_status','lessor_state','lessor_user_id','lessor_name','lessor_mobile','lessor_identify','renter_user_id','renter_name','renter_mobile','renter_identify','type','deposit','unit_price','renter_money','lessor_medium_money','renter_medium_money','renter_pay_money','order_state'])
            ->where('id',$rental_order_id)
            ->find();
        $this->success('查询成功',$rental_order);
    }


    /**
     * 支付
     */
    public function pay(){
        $params = $this->request->param('');
        $platform = array_get($params,'platform','wxmini');
        $order_id = array_get($params,'order_id');
        $pay_type = array_get($params,'pay_type',1);
        if(!$order_id || !$pay_type){
            $this->error('参数错误');
        }
        $type = array_get($params,'type');
        switch($type){
            case 'rental':
                $orderinfo = RentalOrder::get($order_id);
                if(!$orderinfo){
                    $this->error("订单不存在");
                }
                if($orderinfo->pay_status == '1'){
                    $this->error("不要重复支付");
                }
                $orderinfo->pay_type = $pay_type;
                $orderinfo->renter_order_trade_no = 'RT'.date('YmdHis').mt_rand(10,9999);
                $orderinfo->save();
                $order_no = $orderinfo['renter_order_trade_no'];
                $pay_price = $orderinfo['renter_pay_money'];
                $notify_url = request()->domain().'/api/xiluhouse.pay/notify';
                break;
            default:
                $this->error("当前模块不支持支付");
        }
        if($pay_price < 0.01){
            $this->error("小于最低支付金额：0.01");
        }
        if($pay_type == 1){
            #微信支付
            $apptype = $platform == 'wxmini'?1:0;
            $openid = UserThird::where('user_id',$this->auth->id)->where('apptype',$apptype)->value('openid');
            $orderinfo = [
                'body'      =>  '购买商品',
                'order_no'  =>  $order_no,
                'pay_price' =>  config('app_debug')? 0.01 : $pay_price,
                //'pay_price' =>  0.01,
                'notify_url' =>  $notify_url,
                'openid'    =>  $openid,
            ];
            $wx = new Wxmini();
            try {
                $result3 = $wx->union_order($orderinfo);
            }catch (Exception $e){
                $this->error($e->getMessage());
            }
            $this->success('创建微信订单成功',$result3);
        }else{
            $this->success("暂不支持当前支付类型");
        }
    }

    /**
     * 支付回调
     */
    public function notify()
    {
//        Log::record(file_get_contents('php://input'), "notify");
        $response = (new Wxmini())->payment->handlePaidNotify(function ($message, $fail){
            // 你的逻辑
            Log::record($message, "debug");
            $order_no = $message['out_trade_no'];
            Db::startTrans();
            try {
                RentalOrder::pay_notify($order_no);
            }catch (Exception $e){
                Db::rollback();;
                return $e->getMessage();
            }
            Db::commit();
            return true;

        });

        $response->send();
        return;
    }

}